// asyncPackageComponent/components/new_couponList/new_couponList.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    coupons: {
      type: null
    },
    couponShowPopup: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    checkCouponList_new: [],
    use_coupon_size_new: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    chooseCoupon_new(e) {
      const index = e.currentTarget.dataset.index;
      const coupon = e.currentTarget.dataset.coupon;
      if (!coupon.valid) return;
      const checkbox = this.selectComponent(`.coupon-checkboxes-new-${index}`);
      checkbox.toggle();
    },
    couponclose() {
      this.triggerEvent("close")
    },
    async selectCoupon_new(e) {
      let couponBol = e.detail;
      let index = e.currentTarget.dataset.index;
      let item = e.currentTarget.dataset.item;
      console.log(couponBol, index, item)
      this.setData({
        ["coupons[" + index + "].checked"]: couponBol,
      });
      await this._screenCoupon_new(couponBol, item, e.type);
      // console.log(this.data.checkCouponList_new, 'wwwwwwwwwwweeeeee')
    },
    _screenCoupon_new(couponBol, valObj, type) {
      let tarValue = valObj;
      console.log(this.data.checkCouponList_new)
      let checkCouponList_new = this.data.checkCouponList_new;
      if (this.data.checkCouponList_new.length > 0) {
        for (let i = 0; i < this.data.checkCouponList_new.length; i++) {
          if (this.data.checkCouponList_new[i].id == tarValue.id) {
            checkCouponList_new.splice(i, 1);
          }
        }
      }
      if (couponBol) {
        //选中添加
        checkCouponList_new.push(tarValue);
      } else {
        //取消选中
        this.setData({
          cup_notice: true
        });
      }
      this.setData({
        checkCouponList_new: checkCouponList_new,
        use_coupon_size_new: this.data.checkCouponList_new.length
      });
      this.triggerEvent('childSelectCoupon', {
        checkCouponList_new: this.data.checkCouponList_new,
        use_coupon_size_new: this.data.use_coupon_size_new
      })
    },
  }
})